<template>
	<view class="start-index">
		<view :style="{display: currentIndex === 0 ? '' : 'none'}">
			<scroll-view class="custom-tabbar-page" scroll-y enable-back-to-top @scrolltolower="tabbarPageScrollLower">
				<user ref="userRef"></user>
			</scroll-view>
		</view>
		<view v-if="tabberPageLoadFlag[1]" :style="{display: currentIndex === 1 ? '' : 'none'}">
			<scroll-view class="custom-tabbar-page" scroll-y enable-back-to-top @scrolltolower="tabbarPageScrollLower">
				<world ref="worldRef"></world>
			</scroll-view>
		</view>

		<view v-if="tabberPageLoadFlag[2]" :style="{display: currentIndex === 2 ? '' : 'none'}">
			<game ref="gameRef"></game>
		</view>
		
		<view v-if="tabberPageLoadFlag[3]" :style="{display: currentIndex === 3 ? '' : 'none'}">
			<scroll-view class="custom-tabbar-page" scroll-y enable-back-to-top @scrolltolower="tabbarPageScrollLower">
				<message ref="messageRef"></message>
			</scroll-view>
		</view> 
		
		<view v-if="tabberPageLoadFlag[4]" :style="{display: currentIndex === 4 ? '' : 'none'}">
			<mine ref="mineRef"></mine>
		</view>

		<tn-tabbar v-model="currentIndex" :list="tabbarList" activeColor="#838383" inactiveColor="#AAAAAA" activeIconColor="#FBBD12" :animation="true" :safeAreaInsetBottom="true" @change="switchTabbar"></tn-tabbar>
		<loading text="努力加载中.." mask="true" click="true" ref="loading"></loading>
	</view>
</template>

<script>
	import user from '@/pages/index/user/index';
	import world from '@/pages/index/world/index'; 
	import game from '@/pages/index/game';
	import message from '@/pages/index/message';
	import mine from '@/pages/me/index';
    import TnTabbar from '@/components/tn-tabbar/tn-tabbar';
	export default {
		components: {
			TnTabbar,
			user,
			world,
			game,
			message,
			mine,
		},
		data() {
			return {
				userId: 0,
				// 底部tabbar菜单数据
				tabbarList: [{
						title: '首页',
						activeIcon: '/static/tabbar/home_tnnew.png',
						show: '0',
						inactiveIcon: '/static/tabbar/home_tn.png'
					},
					{ 
						title: '动态',
						activeIcon: '/static/tabbar/circle_tnnew.png',
						show: '1',
						inactiveIcon: '/static/tabbar/circle_tn.png'
					},
					{
						title: '匹配',
						activeIcon: '/static/tabbar/game_cur.png',
						inactiveIcon: '/static/tabbar/game.png',
						show: '0',
					},
					{
						title: '私信',
						activeIcon: '/static/tabbar/preferred_tnnew.png',
						inactiveIcon: '/static/tabbar/preferred_tn.png',
						show: '0',
					},
					{
						title: '我的',
						activeIcon: '/static/tabbar/mine_tnnew.png',
						inactiveIcon: '/static/tabbar/mine_tn.png',
						show: '1',
					}
				],
				// tabbar当前被选中的序号
				currentIndex: 1,
				// 自定义底栏对应页面的加载情况
				tabberPageLoadFlag: [],
			}
		}, 
		onLoad(options) {
			var config = uni.getLaunchOptionsSync();
			if(config.scene){
				uni.setStorageSync("scene", config.scene);
			}
			if(options.shareUserId){ 
				uni.setStorageSync("shareUserId", options.shareUserId);
			}
			if(options.wxOpenId){
				uni.setStorageSync("wxOpenId", options.wxOpenId);
			}
			if(options.userId){
				this.$u.route({
					url: 'packgeMine/pages/center/user',
					params: {
						userId: options.userId,
					}
				});
			}
			if(options.qun){
				this.$u.route({
					url: 'pages/index/web',
					params: {
						my: 'true',
					}
				});
			}
			if(options.tuan){
				this.$u.route({
					url: 'packgeMine/pages/tuan/tuan',
					params: {
						my: 'true',
						tuanHomeId: options.tuanHomeId,
					}
				});
			}
			if(options.qunMore){
				this.$u.route({
					url: 'packgeMine/pages/game/more',
					params: {
						my: 'true',
						qunTopicId: options.id,
					}
				});
			}
			this.checkAuth();
			this.checkVersion();
			
			let that = this;
			const index = Number(options.index || that.currentIndex)
			// 根据底部tabbar菜单列表设置对应页面的加载情况
			this.tabberPageLoadFlag = this.tabbarList.map((item, tabbar_index) => {
				return index === tabbar_index
			})
						
			this.switchTabbar(index)
		},
		onShareAppMessage(res) {
			return {
				title: '送你一个对象，快来「处CP」吧！',
				path: '/pages/index?shareUserId='+this.userId,
				imageUrl: 'http://rbtnet.oss-cn-hangzhou.aliyuncs.com/test/20220914/58242695c2a8421b892eced5f1bd3b15.jpeg',
			};
		},
		onShareTimeline() {
			return {
				title: '送你一个对象，快来「处CP」吧！',
				path: '/pages/index?shareUserId='+this.userId,
				imageUrl: 'http://rbtnet.oss-cn-hangzhou.aliyuncs.com/test/20220914/58242695c2a8421b892eced5f1bd3b15.jpeg',
			};
		},
		methods: {
			checkVersion() {
				if (uni.getUpdateManager) {
					const updateManager = uni.getUpdateManager();
					updateManager.onUpdateReady(function(res) {
					    uni.showModal({
					        title: "更新提示",
					        content: "检测到新版本，是否下载新版本并重启小程序？",
							showCancel: false,
					        success(res) {
					            if (res.confirm) {
					                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
					                updateManager.applyUpdate();
					            }
					        }
					    });
					});
					updateManager.onUpdateFailed(function(res) {
					    // 新的版本下载失败
					    uni.showModal({
					        title: "已经有新版本了哟~",
					        content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~",
					        showCancel: false
					    });
					});
				}
			},
			checkAuth() {
				var userId = uni.getStorageSync("userId");
				var scene = uni.getStorageSync("scene");
				if(userId) {
					this.$u.get('/system/user/getWxOpenIdById',{
						userId: userId
					}).then(res => {
						if(res.user.wxOpenId){
							this.$store.commit('setQr', false);
						}
					});
				}
				
				
				this.userId = userId;
				this.$refs.loading.open();
				this.$u.get('/system/config/getMenuValue',{
					appId: this.website.appId,
					scene: scene,
					userId: userId,
				}).then(res => {
					this.$refs.loading.close();
					this.$store.commit('setChatFlag', res.data);
					this.tabbarList[0].show = res.data; 
					//this.tabbarList[2].show = res.data; 
					this.tabbarList[3].show = res.data;
				});
			},
			// 切换导航
			switchTabbar(index) {
				if (index == 0) {
					uni.$emit('indexhome', true)
					this._switchTabbarPage(index)
				}
				if (index == 1) {
					//uni.$emit('indexworld', true)
					this._switchTabbarPage(index)
				}
				if (index == 2) {
					this._switchTabbarPage(index)
				}
				if (index == 3) {
					this.judgeLogin(() => {
						uni.$emit('indexmsg', true)
					});
					this._switchTabbarPage(index)
				}
				if (index == 4) {
					uni.$emit('indexmine', true)
					this._switchTabbarPage(index)
				}
			},
			// 导航页面滚动到底部
			tabbarPageScrollLower(e) {

			},
			// 切换导航页面
			_switchTabbarPage(index) {
				const selectPageFlag = this.tabberPageLoadFlag[index]
				if (selectPageFlag === undefined) {
					return
				}
				if (selectPageFlag === false) {
					this.tabberPageLoadFlag[index] = true
				}
				this.currentIndex = index
			},


		}
	}
</script>


<style lang="scss" scoped>

</style>
